﻿#pragma once

#include <QTreeWidgetItem>
#include <QFileInfo>
#include "UIUtilsExport.h"

namespace Domy {

class FileSystemDisplayProvider;

/**
 * @brief The FileSystemItem class
 * @note  文件系统树状图节点类
 */
class UIUTILS_EXPORT FileSystemItem : public QTreeWidgetItem
{
    friend class FileSystemTreeWidget;
public:
    enum Type { ItemType_Temp = 1000, ItemType_Dir = 1001, ItemType_File = 1002 };

    FileSystemItem(const FileSystemDisplayProvider * const provider, int type)
        : QTreeWidgetItem(type),
          m_provider(provider) {
        this->setFlags(this->flags() | Qt::ItemIsEditable);
    }
    ~FileSystemItem() {}

    /**
     * @brief getText
     * @note  获取显示文本
     */
    QString getText() const;

    /**
     * @brief getName
     * @note  获取名称
     */
    virtual QString getName() const = 0;

    /**
     * @brief setFileInfo
     * @note  设置文件信息
     * @param fi
     */
    void setFileInfo(const QFileInfo &fi);

    /**
     * @brief getFileInfo
     * @note  获取文件信息
     */
    QFileInfo getFileInfo() const;

    /**
     * @brief clearChildren
     * @note  清除子节点
     */
    void clearChildren();

protected:
    virtual void updateDisplay() = 0;

protected:
    QFileInfo m_fi;
    const FileSystemDisplayProvider * const m_provider;
};

/**
 * @brief The DirItem class
 * @note  目录系统树状图节点类
 */
class UIUTILS_EXPORT DirItem : public FileSystemItem
{
    friend class FileSystemTreeWidget;
public:
    DirItem(const FileSystemDisplayProvider * const provider, bool create_temp = true);
    ~DirItem() {}

    virtual QString getName() const override;

    void expand();

    void collapse();

    bool isEmpty() const;

protected:
    virtual void updateDisplay() override;

private:
    void addTempEmptyItem();
};

/**
 * @brief The FileItem class
 * @note  文件系统树状图节点
 */
class UIUTILS_EXPORT FileItem : public FileSystemItem
{
    friend class FileSystemTreeWidget;
public:
    FileItem(const FileSystemDisplayProvider * const provider) : FileSystemItem(provider, ItemType_File) {}
    ~FileItem() {}

    virtual QString getName() const override;

protected:
    virtual void updateDisplay() override;
};

} // namespace Domy
